package com.unitech.drinks.controller;


import com.unitech.drinks.controller.param.PageParam;
import com.unitech.drinks.model.Drink;
import com.unitech.drinks.service.DrinkService;
import com.unitech.drinks.vo.ResponsePageDrinks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

@RestController
public class DrinkController {

    @Autowired
    private DrinkService drinkService;

    @GetMapping(path = "drink")
    @ResponseStatus(code = HttpStatus.OK)
    public ResponsePageDrinks listDrinks(@RequestBody @Valid PageParam pager) {
        return drinkService.listDrinks(pager.getPage(), pager.getSize());
    }

    @GetMapping(path = "drink/")
    @ResponseStatus(code = HttpStatus.OK)
    public ResponsePageDrinks listDrinksTakeSizeParma(@RequestParam int page, @RequestParam int size) {
        return drinkService.listDrinks(page, size);
    }


    @GetMapping(path = "/drink/{id}")
    @ResponseStatus(code = HttpStatus.OK)
    public Drink showDrink(@PathVariable Integer id) {
        return drinkService.showDrink(id);
    }


    @PostMapping(path = "/drink/")
    public Drink createDrink(@RequestBody @Valid Drink drink) {
        drink.setCreateTime(LocalDateTime.now());
        return drink;
    }

    @PutMapping(path = "/drink/")
    public Drink modifyDrink(@RequestBody @Valid Drink drink) {
        return drinkService.modifyDrink(drink);
    }

    @DeleteMapping(path = "/drink/{id}")
    public void deleteDrink(@PathVariable Integer id) {
        drinkService.deleteDrink(id);
    }

}
